'
' Will Strohl (wills@strohlsitedesign.com)
' http://www.willstrohl.com
'
'Copyright (c) 2009, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of StrohlSiteDesign.com nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke
Imports DotNetNuke.Entities.Modules
Imports System.Web.UI
Imports System.Collections.Generic
Imports System.Reflection
Imports System.Text.RegularExpressions

Imports WillStrohl.Modules.FeedHandler.FeedHandlerController

Namespace WillStrohl.Modules.FeedHandler

    ''' <summary>
    ''' This is the view control for the FeedHandler module.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    Partial Class ViewFeeds
        Inherits WNSPortalModuleBase
        Implements DotNetNuke.Entities.Modules.IActionable

#Region " Private Members "

        Private Const c_FeedSourceRegEx As String = "(/Feed.ashx.*)"
        Private Const c_FeedLinkPattern As String = "<a href=""{0}"" target=""_blank"">{1}</a>"

#End Region

#Region " Event Handlers "

        ''' <summary>
        ''' Page_Load runs when the control is loaded
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' </history>
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                If Not Page.IsPostBack Then
                    Me.BindData()
                End If
            Catch exc As Exception ' Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Private Sub dlFeeds_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlFeeds.ItemCommand
            Select Case (e.CommandName)
                Case "EditFeed", "EditRules"
                    Response.Redirect(EditUrl(Null.NullString, Null.NullString, e.CommandName, String.Concat("FeedId=", e.CommandArgument)))
            End Select
        End Sub

#End Region

#Region " Private Methods "

        ''' <summary>
        ''' BindData - this method binds all of the controls
        ''' </summary>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private Sub BindData()
            Me.LocalizePage()

            Dim coll As New FeedInfoCollection
            coll = FeedHandlerController.GetFeeds(Me.ModuleId)

            If Not coll Is Nothing Then
                If coll.Count > 0 Then
                    Me.dlFeeds.DataSource = coll
                    Me.dlFeeds.DataBind()
                    Me.dlFeeds.Visible = True
                    Me.lblNoRecords.Visible = False
                Else
                    Me.dlFeeds.Visible = False
                    Me.lblNoRecords.Visible = True
                End If
            Else
                Me.dlFeeds.Visible = False
                Me.lblNoRecords.Visible = True
            End If
        End Sub

        ''' <summary>
        ''' LocalizePage - this method centralizes the localization of the entire user control
        ''' </summary>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private Sub LocalizePage()
            Me.lblNoRecords.Text = Me.GetLocalizedString("lblNoRecords.Text")
            Me.lblIntro.Text = Me.GetLocalizedString("lblIntro.Text")
        End Sub

#End Region

#Region " Public Methods "

        ''' <summary>
        ''' GetFeedSourceText - returns a formatted link to the feed
        ''' </summary>
        ''' <param name="FeedUrl"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Protected Function GetFeedSourceText(ByVal FeedUrl As String) As String
            ' return the latter portion of the URL
            Return String.Format(c_FeedLinkPattern, FeedUrl, Regex.Match(FeedUrl, c_FeedSourceRegEx).Value)
        End Function

#End Region

#Region " IActionable Implementation "

        Public ReadOnly Property ModuleActions() As DotNetNuke.Entities.Modules.Actions.ModuleActionCollection Implements DotNetNuke.Entities.Modules.IActionable.ModuleActions
            Get
                Dim Actions As New Actions.ModuleActionCollection
                Actions.Add(GetNextActionID, Me.GetLocalizedString("EditFeed.MenuItem.Title"), _
                    String.Empty, String.Empty, String.Empty, _
                    Me.EditUrl("EditFeed"), _
                    False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)
                Return Actions
            End Get
        End Property

#End Region

    End Class

End Namespace
